*Program to merge all dta files in one big dataset

/*
*Run this block if stand alone. If commented out, it is because this will be run from _ricardo_master.do
set more off, perm
clear
graph drop _all
local dir "/Users/rreis/Dropbox/05Shared_folders/HRR/HRR_InflDisaster/Ricardo"  // RR's directory
*local dir "C:\Users\marin\Dropbox\HRR_InflDisaster\Ricardo"  // Marina's directory
cd `dir'
local region = "US" 	// set region US or EZ
*/

local region `1'

* Create dataset with model estimates
******************************
*Read off Daniel estimates
clear
u "$input/`region'_55tails_monthly"
sleep 100
save "$input/`region'_allm", replace

*Merge with Q tails
clear
u "$input/`region'_ZQtails"
merge 1:1 date_ym using "$input/`region'_allm"
drop _merge q*
order date_stata date_ym date
sleep 100
save "$input/`region'_allm", replace

*Merge with N tails
clear
u "$input/`region'_ZNtails"
merge 1:1 date_ym using "$input/`region'_allm"
drop _merge n*
order date_stata date_ym date
sleep 100
save "$input/`region'_allm", replace

*Merge with YQ tails
clear
u "$input/`region'_allm"
merge 1:1 date_ym using "$input/`region'_YQtails"
drop _merge
order date_stata date_ym date
g Ytail4_avg = (Ytail4_y6+Ytail4_y7+Ytail4_y8+Ytail4_y9+Ytail4_y10)/5
g Ytail5_avg = (Ytail4_y6+Ytail4_y7+Ytail4_y8+Ytail4_y9+Ytail4_y10)/5
g Ytail0_avg = (Ytail4_y6+Ytail4_y7+Ytail4_y8+Ytail4_y9+Ytail4_y10)/5
g Ytailm1_avg = (Ytail4_y6+Ytail4_y7+Ytail4_y8+Ytail4_y9+Ytail4_y10)/5
label variable Ytail4_avg 	"Average yoy forward >4"
label variable Ytail5_avg 	"Average yoy forward >5"
label variable Ytail0_avg 	"Average yoy forward <0"
label variable Ytailm1_avg 	"Average yoy forward <m1"

sleep 100
save "$input/`region'_allm", replace

*Risk-adjusted estimates: constant ones
******************************

*Merge with Riskpremia 
  //clear
  //u RiskAdjustments.dta
  //merge 1:1 date_ym using `region'_allm
  //drop _merge
  //order date_stata date_ym date
do "$home/data_RiskAdjustments.do"

*do ./data_RiskAdjustments.do
sleep 500
save "$input/`region'_allm", replace

clear
u "$input/`region'_allm"

*10 year numbers, tails, with risk factor
g p4_10y_pool_c = tail4_10y * RiskFactor
g p4_10y_high_c = tail4_10y * RiskFactor_pos
g p5_10y_pool_c = tail5_10y * RiskFactor
g p5_10y_high_c = tail5_10y * RiskFactor_pos
g p0_10y_low_c = tail0_10y * RiskFactor_neg
g p0_10y_pool_c = tail0_10y * RiskFactor
g pm1_10y_pool_c = tailm1_10y * RiskFactor
g pm1_10y_low_c = tailm1_10y * RiskFactor_neg

*5 year numbers, tails, with risk factor
g p4_5y_pool_c = tail4_5y * RiskFactor
g p4_5y_high_c = tail4_5y * RiskFactor_pos
g p5_5y_pool_c = tail5_5y * RiskFactor
g p5_5y_high_c = tail5_5y * RiskFactor_pos
g p0_5y_low_c = tail0_5y * RiskFactor_neg
g p0_5y_pool_c = tail0_5y * RiskFactor
g pm1_5y_pool_c = tailm1_5y * RiskFactor
g pm1_5y_low_c = tailm1_5y * RiskFactor_neg

*5y5y numbers, tails, with risk factor
g p4_5y5y_pool_c = tail4_5y5y * RiskFactor
g p4_5y5y_high_c = tail4_5y5y * RiskFactor_pos
g p5_5y5y_pool_c = tail5_5y5y * RiskFactor
g p5_5y5y_high_c = tail5_5y5y * RiskFactor_pos
g p0_5y5y_low_c = tail0_5y5y * RiskFactor_neg
g p0_5y5y_pool_c = tail0_5y5y * RiskFactor
g pm1_5y5y_pool_c = tailm1_5y5y * RiskFactor
g pm1_5y5y_low_c = tailm1_5y5y * RiskFactor_neg

sleep 500
save "$input/`region'_allm", replace
